﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace NewAI.Game
{
    public class ServerMessageTypePaser
    {
        public static MessageType Parse(string json)
        {
            json = json.Replace('\n', ' ');
            JObject obj;
            try
            {
                obj = JObject.Parse(json);
            }
            catch (Exception)
            {
                obj = new JObject();
            }
            return StringToMessageType((string)obj["msg_name"]);
        }

        public static string MessageTypeToString(MessageType type)
        {
            switch (type)
            {
                case MessageType.Leg_start:
                    return "leg_start";
                case MessageType.Round:
                    return "round";
                case MessageType.Leg_end:
                    return "leg_end";
                case MessageType.Game_over:
                    return "game_over";
                case MessageType.Nothing:
                    return "nothing";
                default:
                    return "";
            }
        }

        public static MessageType StringToMessageType(string type)
        {
            switch (type)
            {
                case "leg_start":
                    return MessageType.Leg_start;
                case "round":
                    return MessageType.Round;
                case "leg_end":
                    return MessageType.Leg_end;
                case "game_over":
                    return MessageType.Game_over;
                default:
                    return MessageType.Nothing;
            }
        }
    }

}
